package com.itany.shop.controller;

import com.itany.shop.entity.Product;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author:石小俊
 * @Date:2022/12/16 9:25
 * @Version:1.0-SNAPSHOT
 * @Description:
 */
@WebServlet("*.product")
public class ProductServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        if("/findAll.product".equals(path)){
            findAll(request,response);
        }
    }

    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProductService productService = (ProductService) ObjectFactory.getObject("productService");

        try {
            List<Product> products = productService.findAll();
            request.setAttribute("products",products);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("productMsg","服务器内部异常");
        }
        request.getRequestDispatcher("/jsp/main.jsp").forward(request,response);
    }
}
